package com.thinkit.bigdata.web.coremet;

import com.thinkit.bigdata.core.util.excel.XlsUtil;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置任务的一些公共方法
 * Created by hccl on 2018/3/15.
 */
public class ConfTaskPubMet {

    private static Logger logger = Logger.getLogger(ConfTaskPubMet.class);

    /**
     * 判断文件拓展名
     * @param path -
     * @return -
     */
    public static boolean verSuffix(String path) {
        //判断拓展名
        return "xls".equals(XlsUtil.getExt(path)) || "xlsx".equals(XlsUtil.getExt(path)) || "txt".equals(XlsUtil.getExt(path));
    }

    /**
     * 上传xls文件方法
     * @param file -
     * @param url -
     * @return -
     */
    public static boolean verUpload(MultipartFile file, String url) {
        String logTxt = "上传xls文件方法";
        String oldName = file.getOriginalFilename();
        File dir = new File(url);
        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
            logger.info("【" + logTxt + "】文件【" + oldName + "】上传路径创建" + mkdirs);
        }
        File newF = new File(url, oldName);
        try {
            file.transferTo(newF);//上传文件
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * 读取excel / txt中的数据
     * @param path -
     * @return List<StudentBean>
     * @author zhang 2015-08-18 00:08
     */
    public static List<ExcelBean> readExcel(String path, int cellNum) throws IOException {

        if (path != null && !path.equals("")) {
            String ext = XlsUtil.getExt(path);
            if (ext!=null && !ext.equals("")) {
                switch (ext) {
                    case "xls":
                        return XlsUtil.readXls(path, cellNum);
                    case "xlsx":
                        return XlsUtil.readXlsx(path, cellNum);
                    case "txt":
                        return XlsUtil.redTxt(path, cellNum);
                }
            }
        }
        return new ArrayList<>();
    }
}